﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
using Xwang.Task.Model;

namespace Xwang.Task.MvcApp.Controllers
{
    /// <summary>
    /// API控制器，Ajax请求入口
    /// </summary>
    public class AppController : Controller
    {

        public UserInfo LoginUserInfo { get; set; }
        IBll.ITaskJoinUserService TaskJoinUserService { get; set; }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            #region 校验用户是否登录
            LoginUserInfo = Session["LoginUser"] as Model.UserInfo;
            if (LoginUserInfo == null)
            {
                //Ajax请求，请求时验证是否登录，返回登录已过期
                filterContext.HttpContext.Response.AddHeader("Content-Type", "application/json; charset=utf-8");
                filterContext.HttpContext.Response.Write("{\"success\":false,\"msg\":\"登录已过期\"}");
                filterContext.HttpContext.Response.End();
                return;
            }
            #endregion

        }
        //public ActionResult Test()
        //{
        //    return Json(new
        //    {
        //        success = true,
        //        msg = "ok"
        //    }, JsonRequestBehavior.AllowGet);
        //}

        /// <summary>
        /// 获取最新动态,分页，默认20条/页
        /// </summary>
        /// <returns></returns>
        public ActionResult GetFeed(int page = 1)
        {
            //var userid = LoginUserInfo.KeyId;
            return Json(new
            {
                success = true,
                msg = "ok"
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetUncompleted()
        {
            var userid = LoginUserInfo.KeyId;

            var list = TaskJoinUserService.GetUncompleted(userid);

            var t = from a in list
                    select new
                    {
                        LoginUserInfo.UserName,
                        a.Task.KeyId,
                        TaskDesc = a.Task.TaskTitle,
                        a.Task.TaskEndTime
                    };
            return Json(new
             {
                 success = true,
                 msg = "ok",
                 data = t
             }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetExpired()
        {
            var userid = LoginUserInfo.KeyId;

            var list = TaskJoinUserService.GetExpired(userid);

            var t = from a in list
                    select new
                    {
                        LoginUserInfo.UserName,
                        a.Task.KeyId,
                        TaskDesc = a.Task.TaskTitle,
                        a.Task.TaskEndTime
                    };
            return Json(new
            {
                success = true,
                msg = "ok",
                data = t
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetCompleted()
        {
            var userid = LoginUserInfo.KeyId;

            var list = TaskJoinUserService.GetCompleted(userid);

            var t = from a in list
                    select new
                    {
                        LoginUserInfo.UserName,
                        a.Task.KeyId,
                        TaskDesc = a.Task.TaskTitle,
                        a.Task.TaskEndTime
                    };
            return Json(new
            {
                success = true,
                msg = "ok",
                data = t
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
